package com.hn.springframework.webmvc.servlet;

import java.io.File;
import java.util.Locale;

public class HNViewResolver {

    private final String DEFAULT_TEMPLATE_SUFFX = ".html";
    private String templateRootDir;

    public HNViewResolver(String templateRoot) {
        this.templateRootDir = this.getClass().getClassLoader().getResource(templateRoot).getFile();
    }

    public HNView resolveViewName(String viewName, Locale locale) throws Exception {
        if (null == viewName || "".equals(viewName.trim())) { return null; }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFX) ? viewName : viewName + DEFAULT_TEMPLATE_SUFFX;

        File templateFile = new File((this.templateRootDir + "/" + viewName).replaceAll("/+", "/"));

        return new HNView(templateFile);
    }
}
